import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class SymmetricCrypt {

	static ObjectOutputStream encrypt(OutputStream os, String pass)
			throws Exception {
		byte[] key = pass.getBytes();
		DESKeySpec desKeySpec = new DESKeySpec(key);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

		// Create Cipher
		Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
		desCipher.init(Cipher.ENCRYPT_MODE, secretKey);

		// Create outputstream
		BufferedOutputStream bos = new BufferedOutputStream(os);
		CipherOutputStream cos = new CipherOutputStream(bos, desCipher);
		ObjectOutputStream oos = new ObjectOutputStream(cos);

		return oos;
	}

	static ObjectInputStream decrypt(InputStream is, String pass)
			throws Exception {

		byte[] key2 = pass.getBytes();
		DESKeySpec desKeySpec2 = new DESKeySpec(key2);
		SecretKeyFactory keyFactory2 = SecretKeyFactory.getInstance("DES");
		SecretKey secretKey2 = keyFactory2.generateSecret(desKeySpec2);

		// Create Cipher
		Cipher desCipher2 = Cipher.getInstance("DES/ECB/PKCS5Padding");
		desCipher2.init(Cipher.DECRYPT_MODE, secretKey2);

		// Create stream
		BufferedInputStream bis = new BufferedInputStream(is);
		CipherInputStream cis = new CipherInputStream(bis, desCipher2);
		ObjectInputStream ois = new ObjectInputStream(cis);

		return ois;
	}

	public static String EncryptString(String plaintext, String password)
			throws Exception {
		// Create Key
		byte key[] = password.getBytes();
		DESKeySpec desKeySpec = new DESKeySpec(key);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

		// Create Cipher
		Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
		desCipher.init(Cipher.ENCRYPT_MODE, secretKey);

		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		byte[] m = desCipher.doFinal(plaintext.getBytes());
		baos.write(m.length);
		baos.write(m);
		
		baos.close();

		return baos.toString();
	}

	public static String DecryptString(String encryptedText, String password)
			throws Exception {
		// Create Key
		byte key[] = password.getBytes();
		DESKeySpec desKeySpec = new DESKeySpec(key);
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
		SecretKey secretKey = keyFactory.generateSecret(desKeySpec);

		// Create Cipher
		Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
		desCipher.init(Cipher.DECRYPT_MODE, secretKey);

		ByteArrayInputStream bais = new ByteArrayInputStream(
				encryptedText.getBytes());

		byte[] b = new byte[bais.read()];
		bais.read(b);
		bais.close();
		return new String(desCipher.doFinal(b));

	}

}
